home *** CD-ROM | disk | FTP | other *** search
/ Delphi 2.0 - Programmer's Utilities Power Pack / Delphi 2.0 Programmer's Utilities Power Pack.iso / s_to_z / tpack / shells.pas < prev    next >
Encoding:
Pascal/Delphi Source File  |  1996-09-15  |  8.9 KB  |  319 lines

  1. Unit Shells;
  2.  
  3. { DOS/Windows/DLL Shells by Michael Ax; Inspired by Ken Henderson}
  4.  
  5. interface
  6. Uses
  7.   Forms, WinTypes, Controls, Classes, WinProcs, SysUtils, Messages
  8. , PasUtils
  9. , Working
  10. , UserInfo;
  11.  
  12. Const
  13.   ErrorThreshold = 32;
  14.   ShowCommands: array[TWindowState] of Word =(SW_SHOWNORMAL, SW_MINIMIZE, SW_SHOWMAXIMIZED);
  15.   DefaultProcessor = 'COMMAND.COM';
  16.  
  17. Type
  18.  
  19.   TShellOptions = (shlWaitTillDone,shlUseShell,shlMsgTillReady,shlMsgTillDone);
  20.   TShellFlags = set of TShellOptions;
  21.  
  22.   TGenericShell = class(TDialogShell)
  23.   private
  24.     fCommand              : PString;
  25.     fCommandLine          : PString;
  26.     fFlags                : TShellFlags;
  27.     fShellResult          : Word;
  28.     fOnPreShell           : TNotifyEvent;
  29.     fOnPostShell          : TNotifyEvent;
  30.     fOnWait               : TNotifyEvent;
  31.     fWorking              : TWorkingMsg;
  32.   protected
  33.     function DoShell: Word; Virtual;
  34.     function GetCommand:String;
  35.     procedure SetCommand(const Value:String);
  36.     function GetCommandLine:String;
  37.     procedure SetCommandLine(const Value:String);
  38.   public
  39.     constructor Create(AOwner: TComponent); override;
  40.     destructor Destroy; override;
  41.     procedure Notification(AComponent: TComponent; Operation: TOperation); Override;
  42.     procedure Execute; override;
  43.     procedure Run(const aCmd,aParam:String);
  44.     property Command : String read GetCommand write SetCommand;
  45.     property Parameters : String read GetCommandLine write SetCommandLine;
  46.   published
  47.     property Working     : TWorkingMsg read fWorking write fWorking;
  48.     property Flags       : TShellFlags read fFlags write fFlags;
  49.     property ShellResult : Word read fShellResult write fShellResult stored false;
  50.     property OnPreShell  : TNotifyEvent read fOnPreShell write fOnPreShell;
  51.     property OnPostShell : TNotifyEvent read fOnPostShell write fOnPostShell;
  52.     property OnWait      : TnotifyEvent read fOnWait write fOnWait;
  53.     end;
  54.  
  55.   TDLLShell = class(TGenericShell)
  56.   public
  57.     constructor Create(AOwner: TComponent); override;
  58.     function DoShell: Word; Override;
  59.   published
  60.     property Module : String read GetCommand write SetCommand;
  61.     property Proc : String read GetCommandLine write SetCommandLine;
  62.     end;
  63.  
  64.   TWindowsShell = class(TGenericShell)
  65.   private
  66.     fShellResult          : Word;
  67.     fWindowStyle          : TWindowState;
  68.     fOnPreShell           : TNotifyEvent;
  69.     fOnPostShell          : TNotifyEvent;
  70.     fOnWait               : TNotifyEvent;
  71.   protected
  72.     function GetExecStr: String; Virtual;
  73.     function DoShell: Word; Override;
  74.     function GetTest:Boolean; Override;
  75.     procedure SetNoString(const Value:String);
  76.   public
  77.     constructor Create(AOwner: TComponent); override;
  78.   published
  79.     property Command;
  80.     property Parameters;
  81.     property WindowStyle : TWindowState read fWindowStyle write fWindowStyle;
  82.     property ExecString  : String read GetExecStr write SetNoString stored false;
  83.     end;
  84.  
  85.  
  86.   TDosShell = class(TWindowsShell)
  87.   {rather 'ComSpec' shell. if you want to shell using an alternative shell, use WindowsShell}
  88.   private
  89.   protected
  90.     function GetComSpec: String; {returns default if blank}
  91.   public
  92.     constructor Create(AOwner: TComponent); override;
  93.     function GetExecStr: String; override;
  94.   published
  95.     property ComSpec: String read GetComSpec write SetNoString stored false;
  96.     end;
  97.  
  98.  
  99. implementation
  100.  
  101. {-----------------------------------------------------------------------------------------}
  102. { TGenericShell                                                                           }
  103. {-----------------------------------------------------------------------------------------}
  104.  
  105. constructor TGenericShell.Create(AOwner: TComponent);
  106. begin
  107.   inherited Create(AOwner);
  108.   fCommand:=NullStr;
  109.   fCommandLine:=NullStr;
  110. end;
  111.  
  112. destructor TGenericShell.Destroy;
  113. begin
  114.   DisposeStr(fCommandLine);
  115.   DisposeStr(fCommand);
  116.   inherited Destroy;
  117. end;
  118.  
  119. procedure TGenericShell.Notification(AComponent: TComponent; Operation: TOperation);
  120. begin
  121.   inherited Notification(AComponent, Operation);
  122.   if Operation = opRemove then begin
  123.     cx.NilIfSet(fWorking,AComponent);
  124.     end;
  125. end;
  126.  
  127. procedure TGenericShell.Run(const aCmd,aParam:String);
  128. begin
  129.   Command:=aCmd;
  130.   Parameters:=aParam;
  131.   Execute;
  132. end;
  133.  
  134. Procedure TGenericShell.Execute;
  135. begin
  136.   if (fFlags*[shlMsgTillReady,shlMsgTillDone])<>[] then begin
  137.     cx.MakeIfNil(fWorking,TWorkingMsg);
  138.     fWorking.BusyOn;
  139.     end;
  140.  
  141.   if Assigned(fOnPreShell) then
  142.     fOnPreShell(Self);
  143.  
  144.   fShellResult:=DoShell;
  145.  
  146.   if Assigned(fOnPostShell) then
  147.     fOnPostShell(Self); {must decipher error if any}
  148.  
  149.   if fWorking<>nil then begin
  150.     fWorking.BusyOff;
  151.     fWorking:=nil;
  152.     end;
  153.  
  154. end;
  155.  
  156. function TGenericShell.DoShell:Word;
  157. begin
  158.   Result:=0;
  159.   if (shlMsgTillReady in fFlags) and (fWorking<>nil) then begin
  160.     fWorking.BusyOff;
  161.     fWorking:=nil;
  162.     end;
  163. end;
  164.  
  165. {}
  166.  
  167. function TGenericShell.GetCommand:String;
  168. begin
  169.   Result := fCommand^;
  170. end;
  171.  
  172. procedure TGenericShell.SetCommand(const Value:String);
  173. begin
  174.   AssignStr(fCommand, Value);
  175. end;
  176.  
  177. {}
  178.  
  179. function TGenericShell.GetCommandLine:String;
  180. begin
  181.   Result := fCommandLine^;
  182. end;
  183.  
  184. procedure TGenericShell.SetCommandLine(const Value:String);
  185. begin
  186.   AssignStr(fCommandLine, Value);
  187. end;
  188.  
  189. {-----------------------------------------------------------------------------------------}
  190. { TDLLShell                                                                               }
  191. {-----------------------------------------------------------------------------------------}
  192.  
  193. constructor TDLLShell.Create(AOwner: TComponent);
  194. begin
  195.   inherited Create(AOwner);
  196. end;
  197.  
  198. function TDLLShell.DoShell:Word;
  199. var
  200.   DllName,
  201.   ProcName: PChar;
  202.   LinkedProc: Procedure;
  203.   Handle: THandle;
  204. begin
  205. {  Result:=0;
  206.   if not FileExists(Module) then
  207.     raise Exception.Create(classname+': Module '+Module+' does not exist!');}
  208.   if ExtractFileExt(Module)='' then
  209.     DllName:=MakePChar(ChangeFileExt(Module,'.DLL'))
  210.   else
  211.     DllName:=MakePChar(Module);
  212.   try
  213.     Handle:=LoadLibrary(DllName);
  214.     if Handle<ErrorThreshold then
  215.       raise Exception.Create(classname+': Handle for Module '+Module+' is '+inttostr(longint(Handle)));
  216.     ProcName:=MakePChar(Proc);
  217.     try
  218.       TFarProc(@LinkedProc):= GetProcAddress(Handle, ProcName);
  219.       if TFarProc(@LinkedProc)=nil then
  220.         raise Exception.Create(classname+': Module '+Module+' has no procedure '+Proc);
  221.       inherited DoShell; {can turn off message}
  222.       LinkedProc;
  223.     finally
  224.       FreeLibrary(Handle);
  225.       FreePChar(ProcName);
  226.       end;
  227.   finally
  228.     FreePChar(DllName);
  229.     end;
  230. end;
  231.  
  232. {-----------------------------------------------------------------------------------------}
  233. { TWindowsShell                                                                           }
  234. {-----------------------------------------------------------------------------------------}
  235.  
  236.  
  237. constructor TWindowsShell.Create(AOwner: TComponent);
  238. begin
  239.   inherited Create(AOwner);
  240. end;
  241.  
  242. {}
  243.  
  244. function TWindowsShell.DoShell:Word;
  245. var
  246.   P:PChar;
  247. begin
  248.   p:=MakePChar(ExecString);
  249.   Result:=WinExec(p, ShowCommands[fWindowStyle]);
  250.   FreePChar(p);
  251.  
  252.   inherited DoShell; {can turn off message}
  253.  
  254.   If (Result<ErrorThreshold) then
  255.     raise Exception.Create(classname+': DoShell Result '+inttostr(Result));
  256.  
  257.   while (shlWaitTillDone in Flags) and (GetModuleUsage(Result)>0) do begin
  258.     Application.ProcessMessages;
  259.     if Assigned(fOnWait) then
  260.       fOnWait(Self);            {can stop waiting by removing flag}
  261.     end;
  262. end;
  263.  
  264. function TWindowsShell.GetTest:Boolean;
  265. begin
  266.   Result:= fShellResult=0;
  267. end;
  268.  
  269. procedure TWindowsShell.SetNoString(const Value:String);
  270. begin
  271. end;
  272.  
  273. function TWindowsShell.GetExecStr:String;
  274. begin
  275.   Result:=Command+' '+Parameters;
  276. end;
  277.  
  278. {-----------------------------------------------------------------------------------------}
  279. { TDosShell                                                                               }
  280. {-----------------------------------------------------------------------------------------}
  281.  
  282.  
  283. constructor TDosShell.Create(AOwner: TComponent);
  284. begin
  285.   inherited Create(AOwner);
  286.   Include(fFlags,shlUseShell);
  287. end;
  288.  
  289. function TDosShell.GetExecStr:String;
  290. begin
  291.   if shlUseShell in fFlags then
  292.     Result:=ComSpec+' /C'
  293.   else
  294.     Result:='';
  295.   Result:=Result+inherited GetExecStr;
  296. end;
  297.  
  298. function TDosShell.GetComSpec: String;
  299. begin
  300.  
  301. {  IF YOU HAVE WINDOS.DCU or PAS installed in \DELPHI\LIB then please activate the
  302.   lines below.. }
  303.  
  304. {
  305.   Result:=StrPas(GetEnvVar('COMSPEC'));
  306.   if Result='' then
  307. }
  308.  
  309.     Result:=DefaultProcessor;
  310. end;
  311.  
  312.  
  313. {-----------------------------------------------------------------------------------------}
  314. {                                                                                         }
  315. {-----------------------------------------------------------------------------------------}
  316.  
  317. end.
  318.  
  319.